<template>
	<view class="login-container">

		<view class="wechatapp">
			<image class="m-0-a" style="width: 190rpx;height: 190rpx;" :src="src"></image>
		</view>
		<!-- <view class="auth-title">申请获取以下权限</view> -->
		<view v-if="step == 1">
			<!-- <view class="auth-subtitle">获得你的授权进行登录</view> -->

			<view class="login-btn">
				<!-- #ifdef MP-WEIXIN -->
				<button class="btn-normal" @click="getUserInfo">一键登录</button>
				<!-- #endif -->
			</view>

			<view class="no-login-btn">
				<button class="btn-normal" @click="onNotLogin">暂不登录</button>
			</view>

<!-- 			<view class="agreement">
				<checkbox-group @change="checkboxChange">
					<label>
						<checkbox value="同意" style="font-size: 22rpx;"><text>我已阅读并同意</text>
							<text @click.stop="showAgreement = true" style="color: #fcbd71;">友趣UQU用户服务协议、隐私政策</text>
						</checkbox>
					</label>
				</checkbox-group>
			</view> -->
		</view>
		<view v-if="step == 2">
			<!-- <view class="auth-subtitle">使用微信授权手机号进行实名登录</view> -->
			<view class="login-btn">
				
				<!-- #ifdef MP-WEIXIN -->
				<button class="btn-normal" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">填写手机号码</button>
				<!-- #endif -->
			</view>
		</view>



		<u-popup :show="showAgreement" mode="bottom" @close="closeAgreement">
			<scroll-view class="user_agreement" scroll-y="true">
				<u-parse :content="user_agreement"></u-parse>
			</scroll-view>
		</u-popup>
	</view>
</template>

<script>
	import * as UserApi from '@/api/user.js';
	import * as MemberApi from '@/api/member.js'
	export default {
		data() {
			return {
				src: 'https://milkorder.dagger.enpingshi.com/uploadfiles/logo.jpg',
				step: 1,
				background: '',
				agreement: [],
				showAgreement: false,
				listData: [],
				invitation_id: 0,
				user_agreement: ''
			}
		},
		onShow() {
			//wx.login(); //重新登录
		},
		onLoad(e) {
			// 如果存在账号，提醒绑定手机号码
			if (uni.getStorageSync('user_id')) {
				this.step = 2
				uni.setNavigationBarTitle({
					title: '个人信息',
					success: () => {
						
					},
					fail: () => {
						
					},
					complete: () => {
						
					},
				})

			}

			this.invitation_id = uni.getStorageSync('invitation_id') || 0;
			this.member_setting()
		},
		methods: {
			checkboxChange: function(e) {
				this.agreement = e.detail.value
			},
			onNotLogin: function() {
				let currentPagesList = getCurrentPages()
				if (currentPagesList.length <= 1) {
					uni.reLaunch({
						url: '/pages/user/user'
					})
				} else {
					uni.navigateBack();
				}
			},
			closeAgreement() {
				this.showAgreement = false
			},
			member_setting() {
				MemberApi.setting().then(res => {
					this.user_agreement = res.data.agreement
				})
			},

			getUserInfo: function() {
				let self = this;

				// if (this.agreement.length == 0) {
				// 	this.$error('请勾选《用户协议》')
				// 	return
				// }

				wx.getUserProfile({
					lang: 'zh_CN',
					desc: '用于完善会员资料',
					success: (res) => {
						if (res.errMsg !== 'getUserProfile:ok') {
							return false;
						}
						uni.showLoading({
							title: "正在登录",
							mask: true
						});
						// 执行微信登录
					
						wx.login({
							success(res_login) {
								// 发送用户信息
								UserApi.login({
									code: res_login.code,
									user_info: res.rawData,
									encrypted_data: encodeURIComponent(res.encryptedData),
									iv: encodeURIComponent(res.iv),
									signature: res.signature,
									source: 'wx'
								}).then(res => {
									uni.hideLoading();
									if (res.status == 200) {
										// 执行回调函数
										uni.setStorageSync('user_id', res.user_id);
										//self.onNotLogin()
										self.step = 2; 
									}

								})

							}
						});
					}
				});
			},

			getPhoneNumber(e) {
				var self = this;
				if (e.detail.errMsg !== 'getPhoneNumber:ok') {
					return false;
				}
				uni.showLoading({
					title: "正在处理",
					mask: true
				});

				UserApi.bindMobile({
					code: e.detail.code
				}).then(res => {
					uni.hideLoading();
					uni.navigateBack();

				})

			},
		},
	}
</script>

<style>
	.login-container {
		padding: 30rpx;
	}

	.wechatapp {
		padding: 80rpx 0 48rpx;
		border-bottom: 1rpx solid #e3e3e3;
		margin-bottom: 72rpx;
		text-align: center;
	}

	.m-0-a {
		margin: 0 auto;
	}

	.wechatapp .header {
		width: 190rpx;
		height: 190rpx;
		border: 2px solid #fff;
		margin: 0rpx auto 0;
		border-radius: 50%;
		overflow: hidden;
		box-shadow: 1px 0px 5px rgba(50, 50, 50, 0.3);
	}

	.auth-title {
		color: #585858;
		font-size: 34rpx;
		margin-bottom: 40rpx;
	}

	.auth-subtitle {
		color: #888;
		margin-bottom: 88rpx;
		font-size: 28rpx;
	}

	.login-btn {
		padding: 0 20rpx;
	}

	.login-btn button {
		height: 88rpx;
		line-height: 88rpx;
		background: #04be01;
		color: #fff;
		font-size: 30rpx;
		border-radius: 999rpx;
		text-align: center;
	}

	.login-btn button.login-btn-ali {
		height: 88rpx;
		line-height: 88rpx;
		background: #1678ff;
		color: #fff;
		font-size: 30rpx;
		border-radius: 999rpx;
		text-align: center;
	}

	.no-login-btn {
		margin-top: 20rpx;
		padding: 0 20rpx;
	}

	.no-login-btn button {
		height: 88rpx;
		line-height: 88rpx;
		background: #dfdfdf;
		color: #fff;
		font-size: 30rpx;
		border-radius: 999rpx;
		text-align: center;
	}

	.agreement {
		margin-top: 30rpx;
		margin-left: 50rpx;
		margin-bottom: 30rpx;
		font-size: 24rpx;
	}

	.user_agreement {
		height: 820rpx;
		width: 710rpx;
		padding: 20rpx;
	}
</style>